package phi.engine.layers
{
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import phi.engine.actors.ActorOptions;
	import phi.engine.actors.IActor;
	import phi.engine.actors.IVisualActor;
	import phi.engine.core.GameTime;

	public class HScrollLayer extends Layer
	{
		// Private vars
		private var _padding :Number = 0;
		private var _scrollActorClass :Class;
		private var _velocity :Point = new Point();
		private var _velocityDelta :Point = new Point();
		
		/**
		 * Constructor
		 */
		public function HScrollLayer()
		{
			super();
		}
		
		// -----------------------------------------------------------------------------
		// Public Get/Set funcitons
		// -----------------------------------------------------------------------------
		
		public function get velocityDelta():Point
		{
			return _velocityDelta;
		}

		public function set velocityDelta(value:Point):void
		{
			_velocityDelta = value;
		}

		public function get velocity():Point
		{
			return _velocity;
		}

		public function set velocity(value:Point):void
		{
			_velocity = value;
		}

		public function get padding():Number
		{
			return _padding;
		}

		public function set padding(value:Number):void
		{
			_padding = value;
		}

		public function set scrollActorClass( value:Class ):void
		{
			_scrollActorClass = value;
		}
		
		public function get scrollActorClass():Class
		{
			return _scrollActorClass;
		}
		
		// -----------------------------------------------------------------------------
		// Public funcitons
		// -----------------------------------------------------------------------------
		
		override public function update( gameTime:GameTime ):void
		{
			super.update( gameTime );
			
			velocity = velocity.add( velocityDelta );
			
			//destroyUnusedActors();
			createActors();
			scroll();
		}
		
		// -----------------------------------------------------------------------------
		// Protected funcitons
		// -----------------------------------------------------------------------------
		
		protected function destroyUnusedActors():void
		{
			for each( var actor :IVisualActor in actors )
				if( actor.costumeBounds.right < 0 )
					actor.destroy();
		}
		
		protected function createActors():void
		{
			var bounds :Rectangle = getBounds( stage );
			
			if( actors.length == 0 )
				bounds = new Rectangle();
			
			while( bounds.right - 100 < engine.screenWidth )
			{
				// Create actor
				var actor :IActor = new scrollActorClass();
				actor.changeOptions( createActorOptions( new Point( bounds.right, 0 )) );
				
				addActor( actor );
				
				// Update bounds
				bounds = getBounds( stage );
			}
		}
		
		protected function scroll():void
		{
			for each( var actor :IActor in actors )
				DisplayObject( actor ).x -= velocity.x;
		}

		protected function createActorOptions( startPosition:Point ):ActorOptions
		{
			var options :ActorOptions = new ActorOptions();
			options.startPosition = startPosition;
			options.startPosition.x += padding;
			
			return options;
		}
	}
}